/*

	This file loads data from the Statistics for International trade in Goods (abbreviated UHDI by DST)
	Export and import data are loaded separately

*/

cd ${work}1_data

local start	2001
local end	2015
local list	"cvrnr vare land vrd vgt mgd enhedskode enhedskodetekst"

*** EXPORT DATA

*LOAD
foreach y of numlist `start'/`end' {

	use input\uhdi`y' if ie=="2" & cvrnr!="" , clear
		
	keep `list'
	gen year=`y'

	save temp\export_`y', replace	

}

clear
local command use
foreach y of numlist `start'/`end' {
	`command' temp\export_`y'
	erase temp\export_`y'.dta
	local command "append using"
}

*MODIFY
rename cvrnr jurnr	

*convert from dkk to 1000 dkk
foreach v of varlist vrd {
    replace `v'=`v'/1000
}

*COLLAPSE SAME FIRM-PRODUCT-DESTINATION-PRODUCT-UNIT
collapse (sum) vrd vgt mgd , by(jurnr vare land year enhedskode enhedskodetekst)

*SAVE
order jurnr vare land year
sort jurnr vare land year
compress
save output\export_ipdt, replace


*** IMPORT DATA

*LOAD
foreach y of numlist `start'/`end' {

	use input\uhdi`y' if ie=="1" & cvrnr!="" , clear

	keep `list'
	gen year=`y'

	save temp\import_`y', replace	

}

clear
local command use
foreach y of numlist `start'/`end' {
	`command' temp\import_`y'
	erase temp\import_`y'.dta
	local command "append using"
}

*MODIFY
rename cvrnr jurnr	

*convert from dkk to 1000 dkk
foreach v of varlist vrd {
    replace `v'=`v'/1000
}

*COLLAPSE SAME FIRM-PRODUCT-DESTINATION-PRODUCT-UNIT
collapse (sum) vrd vgt mgd , by(jurnr vare land year enhedskode enhedskodetekst)

*SAVE
order jurnr vare land year
sort jurnr vare land year
compress
save output\import_ipdt, replace